unit database_terms;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, ValEdit,
  database_base, fpjson, Dialogs;

type

  TTermDesc = record
    Name: String;
    Path: String;
  end;

  { TDatabaseTermsFrame }

  TDatabaseTermsFrame = class(TDatabaseBaseFrame)
    TermsValueListEditor: TValueListEditor;
    procedure Init; override;
    procedure TermsValueListEditorStringsChange(Sender: TObject);
  private
    TermsInitialised: Boolean;
  public
  end;

implementation

var
  { TODO: make all this stuff translatable (and perhaps re-do it to be more
    like RMMV }
  TermDescriptions: array [0..(10 +10 +26-2  +51) - 1] of TTermDesc = (
    (Name: 'Level (long name)'; Path: 'basic[0]'),
    (Name: 'Level (short name)'; Path: 'basic[1]'),
    (Name: 'Hit points (long name)'; Path: 'basic[2]'),
    (Name: 'Hit points (short name)'; Path: 'basic[3]'),
    (Name: 'Magic points (long name)'; Path: 'basic[4]'),
    (Name: 'Magic points (short name)'; Path: 'basic[5]'),
    (Name: 'Technique points (long name)'; Path: 'basic[6]'),
    (Name: 'Technique points (short name)'; Path: 'basic[7]'),
    (Name: 'Experience (long name)'; Path: 'basic[8]'),
    (Name: 'Experience (short name)'; Path: 'basic[9]'),

    (Name: 'Maximum hit points'; Path: 'params[0]'),
    (Name: 'Maximum magic points'; Path: 'params[1]'),
    (Name: 'Attack force'; Path: 'params[2]'),
    (Name: 'Defence force'; Path: 'params[3]'),
    (Name: 'Magical attack force'; Path: 'params[4]'),
    (Name: 'Magical defence force'; Path: 'params[5]'),
    (Name: 'Agility'; Path: 'params[6]'),
    (Name: 'Luck'; Path: 'params[7]'),
    (Name: 'Hit'; Path: 'params[8]'),
    (Name: 'Evasion'; Path: 'params[9]'),

    (Name: 'Fight'; Path: 'commands[0]'),
    (Name: 'Escape'; Path: 'commands[1]'),
    (Name: 'Attack'; Path: 'commands[2]'),
    (Name: 'Defence'; Path: 'commands[3]'),
    (Name: 'Item'; Path: 'commands[4]'),
    (Name: 'Skill'; Path: 'commands[5]'),
    (Name: 'Equipment (menu option)'; Path: 'commands[6]'),
    (Name: 'Status'; Path: 'commands[7]'),
    (Name: 'Formation'; Path: 'commands[8]'),
    (Name: 'Save'; Path: 'commands[9]'),
    (Name: 'End Game'; Path: 'commands[10]'),
    (Name: 'Options'; Path: 'commands[11]'),
    (Name: 'Weapon'; Path: 'commands[12]'),
    (Name: 'Armour'; Path: 'commands[13]'),
    (Name: 'Key Item'; Path: 'commands[14]'),
    (Name: 'Equip selected (action)'; Path: 'commands[15]'),
    (Name: 'Equip best'; Path: 'commands[16]'),
    (Name: 'Remove all equipment'; Path: 'commands[17]'),
    (Name: 'New Game'; Path: 'commands[18]'),
    (Name: 'Continue'; Path: 'commands[19]'),
    //20 is null
    (Name: 'To Title'; Path: 'commands[21]'),
    (Name: 'Cancel'; Path: 'commands[22]'),
    //23 is null
    (Name: 'Buy'; Path: 'commands[24]'),
    (Name: 'Sell'; Path: 'commands[25]'),

    (Name: 'Always dash'; Path: 'messages.alwaysDash'),
    (Name: 'Remember the command (in battle)'; Path: 'messages.commandRemember'),
    (Name: 'Background music volume'; Path: 'messages.bgmVolume'),
    (Name: 'Background sound volume'; Path: 'messages.bgsVolume'),
    (Name: 'Music effect volume'; Path: 'messages.meVolume'),
    (Name: 'Sound effect volume'; Path: 'messages.seVolume'),
    (Name: 'Possessed now (number of items)'; Path: 'messages.possession'),
    (Name: 'Total experience (%1 number)'; Path: 'messages.expTotal'),
    (Name: 'Experience for the next level (%1 number)'; Path: 'messages.expNext'),
    (Name: 'Save message'; Path: 'messages.saveMessage'),
    (Name: 'Load message'; Path: 'messages.loadMessage'),
    (Name: 'File'; Path: 'messages.file'),
    (Name: 'Party name (%1 leader)'; Path: 'messages.partyName'),
    (Name: 'Enemy emerges (%1 enemy)'; Path: 'messages.emerge'),
    (Name: 'Preemptive attack (%1)'; Path: 'messages.preemptive'),
    (Name: 'Surprise attack'; Path: 'messages.surprise'),
    (Name: 'Escape start'; Path: 'messages.escapeStart'),
    (Name: 'Escape failure'; Path: 'messages.escapeFailure'),
    (Name: 'Victory'; Path: 'messages.victory'),
    (Name: 'Defeat'; Path: 'messages.defeat'),
    (Name: 'Obtained experience'; Path: 'messages.obtainExp'),
    (Name: 'Obtain gold'; Path: 'messages.obtainGold'),
    (Name: 'Obtain item'; Path: 'messages.obtainItem'),
    (Name: 'Level up'; Path: 'messages.levelUp'),
    (Name: 'Obtain skill'; Path: 'messages.obtainSkill'),
    (Name: 'Item used'; Path: 'messages.useItem'),
    (Name: 'Critical hit to an enemy'; Path: 'messages.criticalToEnemy'),
    (Name: 'Critical hit to an actor'; Path: 'messages.criticalToActor'),
    (Name: 'Actor takes damage'; Path: 'messages.actorDamage'),
    (Name: 'Actor is recovering'; Path: 'messages.actorRecovery'),
    (Name: 'actorGain'; Path: 'messages.actorGain'),
    (Name: 'actorLoss'; Path: 'messages.actorLoss'),
    (Name: 'actorDrain'; Path: 'messages.actorDrain'),
    (Name: 'actorNoDamage'; Path: 'messages.actorNoDamage'),
    (Name: 'actorNoHit'; Path: 'messages.actorNoHit'),
    (Name: 'enemyDamage'; Path: 'messages.enemyDamage'),
    (Name: 'enemyRecovery'; Path: 'messages.enemyRecovery'),
    (Name: 'enemyGain'; Path: 'messages.enemyGain'),
    (Name: 'enemyLoss'; Path: 'messages.enemyLoss'),
    (Name: 'enemyDrain'; Path: 'messages.enemyDrain'),
    (Name: 'Enemy no damage'; Path: 'messages.enemyNoDamage'),
    (Name: 'Enemy no hit'; Path: 'messages.enemyNoHit'),
    (Name: 'Evasion'; Path: 'messages.evasion'),
    (Name: 'Magic evasion'; Path: 'messages.magicEvasion'),
    (Name: 'Magic reflection'; Path: 'messages.magicReflection'),
    (Name: 'Counter attack'; Path: 'messages.counterAttack'),
    (Name: 'Substitute'; Path: 'messages.substitute'),
    (Name: 'Buff added'; Path: 'messages.buffAdd'),
    (Name: 'Debuff added'; Path: 'messages.debuffAdd'),
    (Name: 'Buff removed'; Path: 'messages.buffRemove'),
    (Name: 'Action failure'; Path: 'messages.actionFailure')
  );

procedure TDatabaseTermsFrame.Init;
var
  I: Integer;
  TermName, Path, Value: String;
  Terms: TJSONObject;
begin
  TermsValueListEditor.Clear;
  TermsValueListEditor.Row := 0;

  Terms := Db.System['terms'] as TJSONObject;
  TermsValueListEditor.Strings.BeginUpdate;
  for I := High(TermDescriptions) downto Low(TermDescriptions) do begin
    TermName := TermDescriptions[I].Name;
    Path := TermDescriptions[I].Path;
    Value := Terms.FindPath(Path).AsString;
    TermsValueListEditor.InsertRow(TermName, Value, false);
  end;
  TermsValueListEditor.Strings.EndUpdate;
  TermsInitialised := True;
end;

procedure TDatabaseTermsFrame.TermsValueListEditorStringsChange(Sender: TObject
  );
var
  TermId: Integer;
  Path: String;
  Term: TJSONData;//TJSONObject;
  NewValue: String;
begin
  if TermsInitialised then begin
    TermId := TermsValueListEditor.Row - 1;
    Path := TermDescriptions[TermId].Path;

    Term := (Db.System['terms'] as TJSONObject).FindPath(Path);
    NewValue := TermsValueListEditor.Cells[1, TermsValueListEditor.Row];
    Term.Value := NewValue;
    //ShowMessage('Old: ' + Term.AsString + LineEnding + 'New: ' + NewValue + LineEnding + 'Path: ' + Path);
  end;
end;

{$R *.lfm}

end.

